
#include <BPixmap>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BMouseEvent>
#include <BColorDialog>
#include <BCheckBox>
#include <BTextEdit>
#include <BLineEdit>
#include <BButton>
#include <BProgressBar>
#include <BStyle>

#include "PixmapWindow.h"

PixmapWindow::PixmapWindow(const BString& tile) : BMainWindow(tile)
{
	this->setLayered(true);
	this->setBorder(10);
	this->setRadius(20);

	BButton* freeButton = new BButton(Form_IconAlone, "free");
	this->insertTitleButton(0, freeButton);

	BTextEdit* textEdit = new BTextEdit();
	textEdit->setText("click here to popup window!");
	textEdit->setRect(10, 10, 200, 100);

	BHBoxLayout* checkLayout = new BHBoxLayout();
	{
		BCheckBox* layeredCheckBox = new BCheckBox("Layered");
		BCheckBox* increaseCheckBox = new BCheckBox("Increase");
		increaseCheckBox->setName("increase");
		checkLayout->addWidget(layeredCheckBox);
		checkLayout->addWidget(increaseCheckBox);
		connect(layeredCheckBox, Signal_Checked, &PixmapWindow::slotLayeredChecked);
	}

	BHBoxLayout* hlayout = new BHBoxLayout();
	{
		BLineEdit* edit = new BLineEdit();
		edit->setBorder(10);
		edit->setText("Test-edit");
		edit->setRect(230, 0, 300, 100);

		BButton* button = new BButton("Button");
		button->setName("button");
		button->setTips("click here to choose color.");

		hlayout->addWidget(edit, 2);
		hlayout->addWidget(button);
		connect(button, Signal_Clicked, &PixmapWindow::slotChooseColor);
	}

	BProgressBar* progressBar = new BProgressBar();
	progressBar->setName("progress");
	progressBar->setValue(80);
	progressBar->setOrientation(Orientation_Horizontal | Orientation_Reverse);

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addWidget(textEdit, 3);
	layout->addLayout(checkLayout);
	layout->addLayout(hlayout);
	layout->addWidget(progressBar);

	connect(freeButton, Signal_Clicked, &PixmapWindow::slotFreeButtonClicked);
}
PixmapWindow::~PixmapWindow()
{

}

void PixmapWindow::slotLayeredChecked(BObject* object, const BValue& value)
{
	bool layered = value;
	this->setLayered(layered);
}
void PixmapWindow::slotChooseColor()
{
	if (BButton* button = dynamic_cast<BButton*>(this->child("button")))
	{
		BStyle* style = button->style();
		if (!style)
		{
			style = new BStyle();
			button->setStyle(style);
		}
		static BHolder<BColorDialog> colorDialog = new BColorDialog();
		colorDialog->setTitle("Choose color for button background");
		colorDialog->setColor(style->color(Color_Background));
		if (BPixmap* pixmap = this->pixmap())
		{
			pixmap->addWidget(colorDialog);
			if (colorDialog->execute())
			{
				BColor bgColor = colorDialog->color();
				style->setColor(Color_Background, bgColor);
				button->fresh();
			}
			pixmap->removeWidget(colorDialog);
		}
		else
		{
			colorDialog->setParent(0);
			if (colorDialog->execute())
			{
				BColor bgColor = colorDialog->color();
				style->setColor(Color_Background, bgColor);
				button->fresh();
			}
		}
	}
}
void PixmapWindow::slotFreeButtonClicked()
{
	this->setParent(0);
}

void PixmapWindow::updateEvent(const BEvent& event)
{
	if (BCheckBox* checkBox = (BCheckBox*)this->find("increase"))
	{
		if (checkBox->checked())
		{
			BProgressBar* progressBar = (BProgressBar*)this->child("progress");
			progressBar->increase();
			if (progressBar->value() >= progressBar->maximum())
				progressBar->setValue(0);
		}
	}
}

